package com.kefu.chat.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.chat.model.ChatChannel;
import com.kefu.chat.model.ChatPoster;
import com.kefu.chat.service.ChatChannelService;
import com.kefu.chat.service.ChatPosterService;
import com.kefu.chat.vo.*;
import com.kefu.common.context.KefuContext;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.util.Assert;
import com.kefu.common.vo.DeleteVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Api(tags="渠道广告")
@RestController
@RequestMapping("/chat/poster/")
public class ChatPosterController extends AbstractBaseController{
    @Resource
    private ChatPosterService chatPosterService;
    @Resource
    private ChatChannelService chatChannelService;

    /**
     * 添加广告
     * @param chatInfomercialVo
     * @return
     */
    @PostMapping("add")
    @ApiOperation(value="添加广告", notes="添加广告")
    public Result<String> add(@Validated @RequestBody ChatPosterInsertVo chatInfomercialVo) {
        logger.info("添加广告:{}",chatInfomercialVo);
        long count = chatPosterService.count(Wrappers.lambdaQuery(ChatPoster.class).eq(ChatPoster::getTenantId, KefuContext.getTid())
                .eq(ChatPoster::getName, chatInfomercialVo.getName()));
        Assert.isTrue(count > 0, "广告位" + chatInfomercialVo.getName() + "已经存在！");
        ChatPoster info = chatInfomercialVo.convert();
        chatPosterService.save(info);
        return ResultGenerator.genSuccessResult(info.getId());
    }

    /**
     * 修改广告
     * @param chatInfomercialVo
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value="修改广告", notes="修改广告")
    public Result<String> update(@Validated @RequestBody ChatPosterUpdateVo chatInfomercialVo) {
        logger.info("修改广告:{}",chatInfomercialVo);

        List<ChatPoster> list = chatPosterService.list(Wrappers.lambdaQuery(ChatPoster.class).eq(ChatPoster::getTenantId, KefuContext.getTid())
                .eq(ChatPoster::getName, chatInfomercialVo.getName()));
        if (CollUtil.isNotEmpty(list)) {
            boolean flag = true;
            for (ChatPoster infomercial :
                    list) {
                if (!infomercial.getName().equals(chatInfomercialVo.getName())) {
                    flag = false;
                    break;
                }
            }
            Assert.isTrue(!flag, "广告位" + chatInfomercialVo.getName() + "已经存在！");
        }
        ChatPoster info = chatInfomercialVo.convert();
        chatPosterService.update(info);
        return ResultGenerator.genSuccessResult(info.getId());
    }

    /**
     * 删除广告
     * @param delete
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation(value="删除广告", notes="删除广告")
    public Result<String> delete(@Validated @RequestBody DeleteVo delete) {
        logger.info("删除广告:{}",delete);

        List<ChatChannel> list = chatChannelService.list(Wrappers.lambdaQuery(ChatChannel.class)
                .like(ChatChannel::getPosterIds, delete.getId()));
        Assert.isNotEmpty(list, "广告位已经被使用，不能删除！");
        String id = delete.getId();
        chatPosterService.deleteById(id);
        return ResultGenerator.genSuccessResult(delete.getId());
    }

    /**
     * 广告列表
     * @param search
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value="广告列表", notes="广告列表")
    public Result<Page<ChatPoster>> list(@Validated @RequestBody ChatPosterSearchVo search) {
        logger.info("广告列表:{}",search);

        Page<ChatPoster> page = chatPosterService.searchLambdaPage(search);
        return ResultGenerator.genSuccessResult(page);
    }
}


